package controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

import model.forview.ContentChangeByteModelForView;
import model.forview.ContentChangeFloatModelForView;
import model.forview.ContentChangePointModelForView;
import model.forview.ContentChangeStringModelForView;
import model.forview.LoginModelForView;
import model.wrap.type.ByteElement;
import model.wrap.type.FloatElement;
import model.wrap.type.PointElement;
import model.wrap.type.StringElement;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import play.mvc.Result;
import security.Security;
import views.html.contentChangeString;

/**
 * @author pocteg
 * 
 */
public class Content extends Controller
{

    /**
     * ----PRIVATE-----------ATTRIBUTS-----------INSTANCE----------------------
     */

    /**
     * ----PRIVATE-----------ATTRIBUTS-----------STATIC------------------------
     */

    /**
     * ----PROTECTED---------ATTRIBUTS-----------INSTANCE----------------------
     */

    /**
     * ----PROTECTED---------ATTRIBUTS-----------STATIC------------------------
     */

    /**
     * ----PUBLIC------------ATTRIBUTS-----------INSTANCE----------------------
     */

    /**
     * ----PUBLIC------------ATTRIBUTS-----------STATIC------------------------
     */

    public final static String p_getPageContentChangeString_ID_CONTENT = "id_content";

    /**
     * ----CONSTRUCTORS--------------------------------------------------------
     */

    /**
     * ----PRIVATE-----------METHOD-----------INSTANCE-------------------------
     */

    /**
     * ----PRIVATE-----------METHOD-----------STATIC---------------------------
     */

    /**
     * ----PROTECTED---------METHOD-----------INSTANCE-------------------------
     */

    /**
     * ----PROTECTED---------METHOD-----------STATIC---------------------------
     */

    /**
     * ----PUBLIC------------METHOD-----------INSTANCE-------------------------
     */

    /**
     * ----PUBLIC------------METHOD-----------STATIC---------------------------
     */

    /**
     * GET /content/string/change
     * 
     * @author pocteg
     * @param id_content
     * @return
     */
    public static Result getPageContentChangeString(Long id_content)
    {
	StringElement stringElement = new StringElement(id_content);
	ContentChangeStringModelForView forModel = new ContentChangeStringModelForView(stringElement, request().method());
	return ok(contentChangeString.render(forModel));
    }

    /**
     * @author RAMASLOV
     * @return
     */
    public static Result contentChangeString()
    {
	Form<ContentChangeStringModelForView> forModel = form(ContentChangeStringModelForView.class).bindFromRequest();
	StringElement strEl = new StringElement(Long.valueOf(forModel.get().getIdContent()));
	strEl.setContent(forModel.get().getContent());
	strEl.update();
	return redirect(routes.Content.getPageContentChangeString(Long.valueOf(forModel.get().getIdContent())));
    }

    /**
     * 
     * @param id_content
     *            Content for save result
     * @param id_structure
     *            Structure for search
     * @param id_node
     *            Node for mapping
     * @return
     */
    public static Result getPageContentChangePoint(Long id_content, Long id_structure, Long id_node)
    {
	ContentChangePointModelForView point = new ContentChangePointModelForView(id_content, id_structure, id_node);
	return ok(views.html.contentChangePoint.render(point));
    }

    /**
     * 
     * @return
     */
    public static Result contentChangePoint()
    {
	ContentChangePointModelForView requestForm = new ContentChangePointModelForView(form().bindFromRequest().data());
	try
	{
	    Security.isAuthorize();
	    PointElement point = new PointElement(requestForm.id_content);
	    point.setContentNode(requestForm.id_point);
	    point.setContentStructure(requestForm.id_structure);
	    point.update();
	    return redirect(routes.Hierarchy.node(point.getNode().getId(), point.getNode().getStructure().getId()));
	}
	catch (Exception e)
	{
	    return redirect(routes.Application.getPageLogin());
	}
    }

    /**
     * 
     * @param id_content
     * @return
     */
    public static Result getPageContentChangeFloat(Long id_content)
    {
	FloatElement element = new FloatElement(id_content);
	ContentChangeFloatModelForView forModel = new ContentChangeFloatModelForView(element, request().method());
	return ok(views.html.contentChangeFloat.render(forModel));
    }

    /**
     * @author RAMASLOV
     * @return
     */
    public static Result contentChangeFloat()
    {
	Form<ContentChangeFloatModelForView> forModel = form(ContentChangeFloatModelForView.class).bindFromRequest();
	FloatElement strEl = new FloatElement(Long.valueOf(forModel.get().getIdContent()));
	strEl.setContent(Double.valueOf(forModel.get().getContent()).doubleValue());
	strEl.update();
	return redirect(routes.Content.getPageContentChangeFloat(Long.valueOf(forModel.get().getIdContent())));
    }

    /**
     * 
     * @param id_content
     * @return
     */
    public static Result getPageContentChangeByte(Long idContent)
    {
	ByteElement element = new ByteElement(idContent);
	ContentChangeByteModelForView forModel = new ContentChangeByteModelForView(element, request().method());
	return ok(views.html.contentChangeByte.render(forModel));
    }

    /**
     * @author RAMASLOV
     * @return
     */
    public static Result contentChangeByte()
    {
	MultipartFormData body = request().body().asMultipartFormData();
	Form<ContentChangeByteModelForView> forModel = form(ContentChangeByteModelForView.class).bindFromRequest();
	Long idContent = forModel.get().getIdContent();
	FilePart content = body.getFile("content");

	if (null != content && null != idContent)
	{
	    try
	    {
		String fileName = content.getFilename();
		String contentType = content.getContentType();
		File file = content.getFile();
		ByteElement byteElement = new ByteElement(idContent);
		RandomAccessFile accessFile = new RandomAccessFile(file, "r");
		byte[] b = new byte[(int) accessFile.length()];
		accessFile.read(b);
		byteElement.setFile(fileName, contentType, b);
		byteElement.update();
		return redirect(routes.Content.getPageContentChangeByte(idContent));
	    }
	    catch (IOException e)
	    {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}
	    flash("error", "Missing file");
	    return redirect(routes.Content.getPageContentChangeByte(idContent));

	/*
	 * Form<ContentChangeFloatModelForView> forModel =
	 * form(ContentChangeFloatModelForView.class).bindFromRequest();
	 * FloatElement strEl = new
	 * FloatElement(Long.valueOf(forModel.get().getIdContent()));
	 * strEl.setContent
	 * (Double.valueOf(forModel.get().getContent()).doubleValue());
	 * strEl.update(); return
	 * redirect(routes.Content.getPageContentChangeFloat
	 * (Long.valueOf(forModel.get().getIdContent())));
	 */
    }

    /**
     * @author RAMASLOV
     * @return
     */
    public static Result contentByteDownload(Long idContent)
    {
	ByteElement byteElement = new ByteElement(idContent);
	if(null == byteElement || null == byteElement.getContent())
	    return notFound();
	response().setContentType(byteElement.getContentType());
	response().setHeader("Content-Disposition", "attachment; filename="+byteElement.getFileName() );
	return ok(byteElement.getContent());
    }
    /**
     * ----GETTERS--&--SETTERS-------------------------------------------------
     */
}
